package com.example.movie.ui.user.adapter

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.app.NavUtils
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.movie.R
import com.example.movie.common.constants.AppConstants
import com.example.movie.common.constants.Constants
import com.example.movie.data.movie.model.vo.SearchVo
import com.example.movie.ui.movie.MovieDetailActivity
import com.example.movie.utils.ContextHolder
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item_constellation.*
import kotlinx.android.synthetic.main.item_search.view.*

class MyMovieListAdapter(
    private val items: List<SearchVo>,
    private val typeList: Int,
    private val activity: FragmentActivity
) : RecyclerView.Adapter<MyMovieListAdapter.MyViewHolder>() {

    class MyViewHolder(view: View) :
        RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_search, parent, false)
        )
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        with(holder.itemView) {
            items[position].apply {
                this@with.movie_name.text = this.movieName
                this@with.type.text = "评分 " + this.score.toString()
                this@with.actors.text = this.actors
                this@with.playDay.text = this.registerTime + "上映"

                Glide
                    .with(context)
                    .load(AppConstants.STATIC_SOURCE_URL + this.cimg)
                    .placeholder(R.mipmap.logo)
                    .error(R.drawable.error)
                    .into(this@with.movie_portrait as ImageView)

                this@with.setOnClickListener {
                    with(Bundle()) {
                        putInt("movieId", this@apply.movieId)
                        val intent = Intent(activity, MovieDetailActivity::class.java)
                        intent.putExtras(this)
                        activity.startActivity(intent)
                    }
                }

                when (typeList) {
                    Constants.UserInfo.HOPE_LOOK_LIST -> {
                        this@with.hopeLookNum.text = hopeLook.toString() + "人想看"
                        wantSee.text = "购票"
                        wantSee.setOnClickListener {
                            ContextHolder.getMainContext().location = Constants.JumpAction.toCinemaFragment
                            NavUtils.navigateUpFromSameTask(activity)
                        }
                    }
                    Constants.UserInfo.HAVE_SEEN_LIST -> {
                        this@with.hopeLookNum.text = haveSeen.toString() + "人看过"
                        wantSee.visibility = View.GONE
                    }
                }
            }
        }
    }

    override fun getItemCount() = items.size

}